let tabs = [];
let activeTab = null;

// 导航菜单点击
document.querySelectorAll('.nav-link').forEach(link => {
	link.addEventListener('click', function(e) {
		e.preventDefault();
		const src = this.dataset.src;
		const title = this.dataset.title;
		openTab(src, title);
		if(isMobile()){
			document.querySelector('.nav-container').classList.toggle('active');
		}
	});
});

if(isMobile()){
	$('a[data-src*="mobile"]').css('display','block');
	$('a:not([data-src*="mobile"])').css('display','none');
}else{
	$('a[data-src*="mobile"]').css('display','none');
	$('a:not([data-src*="mobile"])').css('display','block');
}
$('a[data-src*="pdf"]').css('display','block');

// 打开新标签页
function openTab(src, title) {
	// 检查是否已存在
	const existingTab = tabs.find(tab => tab.src === src);
	if (existingTab) {
		activateTab(existingTab.id);
		return;
	}

	const tabId = Date.now().toString();
	const tab = {
		id: tabId,
		src: src,
		title: title,
		element: createTabElement(tabId, title),
		iframe: createIframe(src)
	};

	tabs.push(tab);
	activateTab(tabId);
}

// 创建标签元素
function createTabElement(id, title) {
	const tab = document.createElement('div');
	tab.className = 'tab-item';
	tab.id = id;
	tab.innerHTML = `
                ${title}
                <span class="tab-close" onclick="closeTab('${id}', event)">×</span>
            `;

	tab.addEventListener('click', () => activateTab(id));
	tab.addEventListener('contextmenu', showContextMenu);

	// 移动端长按事件
	let timer;
	tab.addEventListener('touchstart', (e) => {
		timer = setTimeout(() => showContextMenu(e), 500);
	});
	tab.addEventListener('touchend', () => clearTimeout(timer));

	document.getElementById('tabBar').appendChild(tab);
	return tab;
}

// 创建iframe
function createIframe(src) {
	const iframe = document.createElement('iframe');
	iframe.src = src;
	iframe.style.display = 'none';
	document.getElementById('iframeContainer').appendChild(iframe);
	return iframe;
}

// 激活标签页
function activateTab(id) {
	tabs.forEach(tab => {
		const isActive = tab.id === id;
		tab.element.classList.toggle('active', isActive);
		tab.iframe.style.display = isActive ? 'block' : 'none';
	});
	activeTab = id;
}

// 关闭标签页
function closeTab(id, e) {
	e.stopPropagation();
	const index = tabs.findIndex(tab => tab.id === id);
	if (index === -1) return;

	const tab = tabs[index];
	tab.element.remove();
	tab.iframe.remove();
	tabs.splice(index, 1);

	if (tabs.length > 0) {
		activateTab(tabs[tabs.length - 1].id);
	}
}

// 右键菜单
function showContextMenu(e) {
	e.preventDefault();
	const menu = document.getElementById('contextMenu');
	menu.style.display = 'block';
	menu.style.left = `${e.clientX}px`;
	menu.style.top = `${e.clientY}px`;
}

// 关闭其他
function closeOthers() {
	tabs = tabs.filter(tab => tab.id === activeTab);
	updateTabs();
}

// 关闭全部
function closeAll() {
	tabs = [];
	updateTabs();
}

// 更新标签页显示
function updateTabs() {
	document.getElementById('tabBar').innerHTML = '';
	document.getElementById('iframeContainer').innerHTML = '';
	tabs.forEach(tab => {
		document.getElementById('tabBar').appendChild(tab.element);
		document.getElementById('iframeContainer').appendChild(tab.iframe);
	});
}

// 移动端菜单切换
function toggleNav() {
	document.querySelector('.nav-container').classList.toggle('active');
}

// 点击外部关闭菜单
document.addEventListener('click', () => {
	document.getElementById('contextMenu').style.display = 'none';
});

function isMobile() {
  const ua = navigator.userAgent;
  return /(iPhone|iPad|iPod|Android|Mobile|BlackBerry|Windows Phone)/i.test(ua);
} 
